<?php

class ProgramsController extends Controller
{
    public $layout='//layouts/main';

    public function actions(){
      return array(
          'captcha'=>array(
              'class'=>'CaptchaExtendedAction',
          ),
      );
    }

    public function accessRules()
    {
      return array(
        array('allow',
          'actions'=>array('show','form','captcha', 'index'),
          'users'=>array('*'),
        ),
      );
    }

    public function actionIndex()
    {
      $forbidden = array('forum', 'ode');

      //$name = strtolower($slug);
      $name = strtolower($_GET['name']);
      $program = Programs::model()->find('name = :name', array(':name' => $name));

      $form_link = "#";
      $name = strtolower($program->name);
      if(in_array($name, $forbidden) == false)
      {
          $form_link = bu('programs/form/'.$name);
      }

      $this->render('show', array(
        'program' => $program,
        'form_link' => $form_link,
      ));
    }

    public function actionForm($name)
    {
        $with_details = array('activities');
    
        $slug = strtolower($name);
        $program = Programs::model()->find('name = :name', array(':name' => $slug));
        
        $class = ucfirst($slug);
        $model = new $class;
        
        $detail = '';
        if(in_array($slug, $with_details) == true){
          $class_detail = $class.'Detail';
          $detail = new $class_detail;
        }

        $this->render($name, array(
          'model' => $model,
          'detail' => $detail
        ));
    }
}